package br.usp.ime.origami.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

public class MovingTriangulation {

	private Queue<MovingTriangle> queue = new PriorityQueue<MovingTriangle>(11, new ColapsingTimeComparator());

	public MovingTriangulation() {
	}

	public MovingTriangulation(List<MovingTriangle> triangles) {
		this.queue.addAll(triangles);
	}

	/**
	 * 
	 * @return all triangles in collapsing time order
	 */
	public List<MovingTriangle> getMovingTriangles() {
		// TODO: bad, always recreating.
		return Arrays.asList(queue.toArray(new MovingTriangle[queue.size()]));
	}

	public List<MovingTriangle> getNotCollapsedMovingTrianglesAt(double time) {
		List<MovingTriangle> list = new ArrayList<MovingTriangle>();
		for (MovingTriangle t : queue) {
			if (t.getCollapsingTime() > time) {
				list.add(t);
			}
		}
		return list;
	}

	public void add(MovingTriangle mt) {
		this.queue.add(mt);
		System.out.println(this.queue.size());
	}

	public boolean hasMoreCollapsingTriangles() {
		if (this.queue.isEmpty()) {
			return false;
		}
		return this.queue.peek().getCollapsingTime() != Double.MAX_VALUE;
	}

	public MovingTriangle nextCollapsingTriangle() {
		if (!hasMoreCollapsingTriangles()) {
			throw new IllegalStateException("no more collapsing triangles!");
		}
		return nextTriangle();

	}

	public MovingTriangle nextTriangle() {
		return this.queue.remove();
	}

	public int size() {
		return queue.size();
	}

}

